class  Queue{
    constructor(){
        this.items ={};//存放队列的元素
        this.count = 0;//键个数
        this.lowestCount = 0;//出队元素的起始下标
    }
    //入队
    enqueue(element){
        this.items[this.count] = element;
        this.count++; 
    }
    //出队
    dequeue(){
        if(this.isEmpty()){
            return undefined
        }
        let target = this.items[this.lowestCount];
        delete this.items[this.lowestCount]    
        this.lowestCount++;
        return target
    }
    //返回队列第一个元素
    peek(){
        return this.items[this.lowestCount]
    }
    //是否为空
    isEmpty(){
        return this.count - this.lowestCount === 0 
    }
    //个数
    size(){
        return this.count - this.lowestCount
    }
    //清空队列
    clear(){
        this.items= {};
        this.count =0;
        this.lowestCount =0;
    }
    //转字符串
    toString(){
        if(this.isEmpty()){
            return ''
        }
        let str = this.items[this.lowestCount];
        for(let i = this.lowestCount + 1  ; i < this.count ; i++){
            str = `${str},${this.items[i]}`
        }
        return str
    }

}
// let q = new Queue()
// q.enqueue('a')
// q.enqueue('b')
// q.enqueue('c')
// q.dequeue()
// console.log(q.toString())
// console.log(q)

//约瑟夫环 丢手绢 击鼓传花
function flower(arr , num){
    let Q = new  Queue();
    let list = [];
    for(let i = 0 ; i < arr.length ; i++){
        Q.enqueue(arr[i])
    }
    while(Q.size() > 1){
        for(let j = 0 ; j < num - 1 ; j++){
            Q.enqueue(Q.dequeue())
        }
        list.push(Q.dequeue())
    }
    let cur = Q.dequeue()
    console.log(list,cur)
}
flower(['a','b','c','d'],2)
